﻿using System;
using System.Collections.Generic;
using UnityEngine;

public static class SkillArea
{
    public abstract class Base
    {
        public SkillBase skill;
        public virtual List<UnitEntityBase> SearchArea() => default;
    }

    public class None : Base { }

    /// <summary>
    /// 矩形区域
    /// </summary>
    public class Rectangle : Base
    {
        public float left { get; private set; }
        public float right { get; private set; }
        public float bottom { get; private set; }
        public float top { get; private set; }
        public Vector2 center { get; private set; }

        public Rectangle(SkillBase skill, float left, float right, float bottom, float top)
        {
            this.skill = skill;
            this.left = left;
            this.right = right;
            this.bottom = bottom;
            this.top = top;
            this.center = new Vector2(left + right, bottom + top) / 2;
        }

        public override List<UnitEntityBase> SearchArea() => SearchRect(skill.entity, left, right, bottom, top);

        /// <summary>
        /// 以碰撞盒中心为中心在矩形范围内搜索目标
        /// </summary>
        public List<UnitEntityBase> SearchRect(UnitEntityBase source, float leftOffset, float rightOffset, float bottomOffset, float topOffset, Func<UnitEntityBase, bool> condition = null)
        {
            m_SearchRectTargets.Clear();
            var centerOffset = new Vector2(rightOffset + leftOffset, topOffset + bottomOffset) / 2;
            var rectSize = new Vector2(rightOffset - leftOffset, topOffset - bottomOffset);
            var offset = centerOffset * source.animator.direction;
            var point = (Vector2)source.transform.position + source.physics.box.offset + offset;
            var layers = Physics2D.GetLayerCollisionMask(source.gameObject.layer + 1);
            m_Collis = Physics2D.OverlapBoxAll(point, rectSize, 0f, layers);
            foreach (var colli in m_Collis)
            {
                var target = colli.GetComponent<UnitEntityBase>();
                if (target == null) continue;
                if (condition == null || condition.Invoke(target))
                {
                    m_SearchRectTargets.Add(target);
                }
            }
            return m_SearchRectTargets;
        }
        private Collider2D[] m_Collis;
        private List<UnitEntityBase> m_SearchRectTargets = new List<UnitEntityBase>();
    }

    /// <summary>
    /// 扇形区域，当 rangeAngle >= 360 时为圆形
    /// </summary>
    //public class Sector : Base
    //{
    //    public float centerX, centerY, radius, rangeAngle, dirAngle;
    //    public Sector(SkillBase skill, float centerX, float centerY, float radius, float rangeAngle = 0f, float dirAngle = 0f)
    //    {
    //        this.skill = skill;
    //        this.centerX = centerX;
    //        this.centerY = centerY;
    //        this.radius = radius;
    //        this.rangeAngle = Mathf.Clamp(rangeAngle, 0f, 360f);
    //        this.dirAngle = Mathf.Clamp((dirAngle - 180f) % 360f + 180f, -180f, 180f);
    //    }
    //}
}               